<?php
namespace App\Model;
use PhalApi\Model\DataModel;

class MirContent extends DataModel {

    protected function getTableName($id) {
        return 'game_content';
    }

    //插入数据
    public function selectByCategoryList($data) {
        $rides = \PhalApi\DI()->cache->get( $data->Category."|".$data->CategoryTitle);
        if( $rides ){
            return $rides;
        }
        $list = $this->getORM()->where(
            array(
                'Category' => $data->Category,
                'CategoryTitle'=> $data->CategoryTitle
            ))->fetchAll();

        \PhalApi\DI()->cache->set( $data->Category."|".$data->CategoryTitle,$list,60*24*60*60 );
        return $list;
    }

    //插入数据
    public function selectByPageName($data) {

        $page = \PhalApi\DI()->cache->get( $data->Category."|".$data->CategoryTitle."|".$data->PageName );
        if ( $page ){

        }
        //读出数据
        $page = $this->getORM()->where( array(
            'Category' => $data->Category,
            'CategoryTitle'=> $data->CategoryTitle,
            'PageName'=> $data->PageName,
        ))->fetchOne();
        //存入 redis
        \PhalApi\DI()->cache->set( $data->Category."|".$data->CategoryTitle."|".$data->PageName,$page,60*24*60*60 );

        return $page;
    }

    //插入数据基函数
    public function insertMir($data) {
        return $this->getORM()->insert($data);
    }

    //插入数据, 重构数据
    public function insertPageName($json) {
        $json['update_time'] = date("Y-m-d H:i:s",time());
        $json['create_time'] = date("Y-m-d H:i:s",time());

        $mode = new MirCategory();
        $category = $mode->selectByWhere($json);

        $mode = new MirCategoryList();
        $categoryList = $mode->selectByWhere($json);

        $cotent = $this->insertMir($json);

        return array(
            $category,
            $categoryList,
            $cotent,
        );
    }

}
